package com.itheima.appserver.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.appserver.config.NoAuthorization;
import com.itheima.appserver.service.ManageService;
import com.itheima.commons.model.vo.ManageTopVo;
import com.itheima.commons.model.vo.ManageVo;
import com.itheima.commons.model.vo.PageResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lqTao
 * @Date: 2021/12/16 17:56
 */
@RestController
@RequestMapping("/management/manage")
public class ManageController {
    @Autowired
    ManageService manageService;

    /**
     * 视频记录翻页
     * @param page      页尺寸
     * @param pageSize  页数
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param uid       用户ID
     * @return 结果集
     */
    @GetMapping("videos")
    public ResponseEntity videosManage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "sortProp") String sortProp,
                                       @RequestParam(value = "sortOrder", defaultValue = "ascending") String sortOrder,
                                       @RequestParam(value = "uid") Integer uid) {
        PageResult pageResult = manageService.videoList(page, pageSize, sortProp, sortOrder, uid);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 日志翻页
     * @param page      页尺寸
     * @param pageSize  页数
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param type      类型 01 用户登录
     * @param uid       用户ID
     * @return 结果集
     */
    @GetMapping("logs")
    public ResponseEntity logsManage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "sortProp") String sortProp,
                                     @RequestParam(value = "sortOrder", defaultValue = "ascending") String sortOrder,
                                     @RequestParam(value = "type", defaultValue = "诺基亚") String type,
                                     @RequestParam(value = "uid") Integer uid) {
        PageResult pageResult = manageService.logsList(page, pageSize, sortProp, sortOrder, type, uid);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 评论列表翻页
     * @param page      页尺寸
     * @param pageSize  页数
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @return 结果集
     */
    @GetMapping("messages/comments")
    public ResponseEntity commentsManage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "sortProp") String sortProp,
                                         @RequestParam(value = "sortOrder", defaultValue = "ascending") String sortOrder,
                                         @RequestParam(value = "messageID") ObjectId id) {
        PageResult pageResult = manageService.commentsList(page, pageSize, sortProp, sortOrder, id);
        return ResponseEntity.ok(pageResult);
    }

    //消息详情和消息翻页
    @GetMapping("/messages")
    public ResponseEntity messagesList(@RequestParam Map<String, String> map) {
        TimeInterval timer = DateUtil.timer();
        System.out.println("开始计时");

        //api文档写了，但是没传过来
        String userID = map.get("userID");
        String reasonsForThawing = map.get("reasonsForThawing");

        //以下为固定通用
        String sortProp = map.get("sortProp");
        String sortOrder = map.get("sortOrder");
        Integer page = Convert.toInt(map.get("page"), 1);
        Integer pagesize = Convert.toInt(map.get("pagesize"), 10);

        PageResult pageResult;
        Long uid = Convert.toLong(map.get("uid"));  //uid为用户id
        if (ObjectUtil.isNotNull(uid)) {
            //uid不为空时，查看某一用户的圈子，且能看到置顶功能
            pageResult = manageService.messages(uid, page, pagesize,sortProp,sortOrder);
        } else {
            //动态审核
            String id = map.get("id");//id为用户id
            String state = map.get("state");
            String sd = map.get("sd");
            String ed = map.get("ed");
            pageResult = manageService.messagesList(id, page, pagesize, sd, ed, state);
        }
        long interval = timer.interval();
        System.out.println("计时结束，花了" + interval + "毫秒");
        return ResponseEntity.ok(pageResult);
    }

    //消息通过

    @PostMapping("/messages/pass")
    public ResponseEntity messagesPass(@RequestBody String[] publishIds) {

        HashMap<String, String> map = new HashMap<>();
        String message ;

        if (manageService.messagesPass(publishIds)) {
            message = "ok";
        } else {
            message = "fail";
        }

        map.put("message", message);
        return ResponseEntity.ok(map);
    }

    //消息拒绝
    @PostMapping("/messages/reject")
    public ResponseEntity messagesReject(@RequestBody String[] publishIds) {
        HashMap<String, String> map = new HashMap<>();
        manageService.messagesReject(publishIds);
        map.put("message", "操作成功");
        return ResponseEntity.ok(map);
    }

    //消息置顶
    @PostMapping("/messages/{id}/top")
    public ResponseEntity messagesTop(@PathVariable("id") String publishId) {
        HashMap<String, String> map = new HashMap<>();
        manageService.messagesTop(publishId);
        map.put("message", "操作成功");
        return ResponseEntity.ok(map);
    }

    //单条消息，置顶和取消置顶时都会发送该请求,展示置顶按钮
    @GetMapping("/messages/{id}")
    public ResponseEntity oneMessages(@PathVariable("id") ObjectId id) {
        ManageTopVo manageTopVo = manageService.findOneMessages(Convert.toStr(id));
        return ResponseEntity.ok(manageTopVo);
    }

    //消息取消置顶
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity messagesUntop(@PathVariable("id") String publishId) {
        HashMap<String, String> map = new HashMap<>();
        manageService.messagesUntop(publishId);
        map.put("message", "操作成功");
        return ResponseEntity.ok(map);
    }

    //消息撤销
    @PostMapping("/messages/revocation")
    public ResponseEntity messagesRevocation(@RequestBody String[] publishIds) {
        HashMap<String, String> map = new HashMap<>();
        manageService.messagesRevocation(publishIds);
        map.put("message", "操作成功");
        return ResponseEntity.ok(map);
    }
}
